import { createRouter, createWebHashHistory } from "vue-router";
// import Layout from '@/components/Layout/index.vue'
import userMenu from "@/store/module/menu";
// 解构store中的数据，使之成为响应式数据
import { storeToRefs } from 'pinia'

const isLogin = true
let hasRoles = true
export const routes = [
  {
    path: '/login',
    name: 'Login',
    hide:true,
    component: () => import('@/views/Login/index.vue')
  },
]

const router = createRouter({
  scrollBehavior: () => ({ left: 0, top: 0 }),
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const menu = userMenu()
  //解决动态添加路由刷新页面空白问题
    if (hasRoles) {
      const userRoute = [
        {
          path: '/',
          name: 'Home',
          component: 'components/Layout/index.vue',
          redirect: '/home',
          icon:'home',
          title:'首页',
          children: [
            {
              path: '/home',
              name: 'Home',
              component: 'views/Home/index.vue',
              hide:false,
              icon:'home',
              title:'首页'
            },
            {
              path: '/home/page1',
              name: 'Page1',
              component: 'views/Page1/index.vue',
              hide:false,
              icon:'menuItem',
              title:'页面1'
            },
            {
              path: '/home/page2',
              name: 'Page2',
              component: 'views/Page2/index.vue',
              hide:false,
              icon:'menuItem',
              title:'页面2'
            },
            {
              path: '/home/page3',
              name: 'Page3',
              component: 'views/Page3/index.vue',
              hide:false,
              icon:'menuItem',
              title:'页面3'
            },
            {
              path: '/user',
              name: 'User',
              component: 'views/User/index.vue',
              hide:false,
              icon:'user',
              title:'用户管理'
            },
          ],
        },
        {
          path: '/goods',
          name: 'Goods',
          component: 'components/Layout/index.vue',
          redirect: '/page4',
          hide:true,
          icon:'tools',
          title:'商品管理',
          children: [
            {
              path: '/goods/page4',
              name: 'Page4',
              component: 'views/Page4/index.vue',
              hide:false,
              icon:'tools',
              title:'页面4'
            },
            {
              path: '/goods/page5',
              name: 'Page5',
              component: 'views/Page5/index.vue',
              hide:false,
              icon:'menuItem',
              title:'页面5'
            },
          ]
        },
        {
          path: '/:pathMatch(.*)*',
          name: '404',
          component: 'views/404/index.vue',
          hide:true
        }
      ]
      menu.changeMenuList(userRoute)
      menu.addRoute()
      hasRoles = false
      next({...to,replace:true})
    }else{
      next()
    }
})

export default router